import 'dart:ui';
import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/parameters.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_examine_lib/examine_list/examine_list_bloc.dart';
import 'package:doctor_examine_lib/examine_list/examine_list_entity.dart'
    as ExamineBean;
import 'package:flutter/material.dart';

class ExamineListPage extends BaseBlocPage<ExamineListBloc, _ExamineListState> {
  ExamineListPage(Parameters parameters) : super(parameters);

  @override
  _ExamineListState generateThemeState() {
    return _ExamineListState();
  }
}

class _ExamineListState extends BaseBlocPageState<ExamineListBloc> {
  void initState() {
    super.initState();
    bloc.initailDatas(parameters.getString("hospitalID"));
  }

  @override
  ignoreWindowWidget() => false;

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        title: Text("添加检查项目"),
        actions: [],
      ),
      backgroundColor: theme.themeColor.themeBgColor,
      body: SafeArea(
          child: StreamBuilder<List<ExamineBean.Datum>>(
              stream: bloc.firstDataSourceSubject,
              builder: (context, snapshot) {
                return snapshot.hasData
                    ? Padding(
                        padding: const EdgeInsets.only(top: 8.0),
                        child: snapshot.hasData
                            ? ListView.separated(
                                itemBuilder: (BuildContext context, int index) {
                                  return Container(
                                    color: Colors.white,
                                    child: ListTile(
                                        trailing: Icon(Icons.arrow_forward_ios,
                                            size: 16),
                                        title: Text((snapshot.data)[index]
                                            .examineClassName),
                                        onTap: () {
                                          bloc.fetchSecondItemsByClass(
                                              (snapshot.data)[index]);
                                          showModalBottomSheet(
                                              context: context,
                                              builder: (BuildContext context) {
                                                return _modalBottomViewBuilder(
                                                    context, theme);
                                              }).then((value) {
                                            if (value != null) {
                                              Routers.pop(context, value);
                                            }
                                          });
                                        }),
                                  );
                                },
                                separatorBuilder: (context, index) {
                                  return Divider(
                                    height: 10,
                                    thickness: 10,
                                    color: ColorUtil.color("#f6f6f6"),
                                  );
                                },
                                itemCount: snapshot.data.length)
                            : Container())
                    : buildLoadingIndicator();
              })),
    );
  }

  // 底部模态弹框 （二三级界面）
  Widget _modalBottomViewBuilder(context, MTheme theme) {
    return Container(
      width: double.infinity,
      color: Colors.red,
      child: Row(
        children: [
          _secondListViewBuilder(context, theme),
          Expanded(child: _thirdListViewBulider(context))
        ],
      ),
    );
  }

  // 二级界面构建
  Widget _secondListViewBuilder(context, theme) {
    return Container(
      width: MediaQuery.of(context).size.width / 3,
      color: ColorUtil.color("#f6f6f6"),
      child: StreamBuilder<List<String>>(
        stream: bloc.secondDataSourceSubject,
        builder: (context, snapshot) {
          return snapshot.hasData
              ? ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      onTap: () {
                        bloc.selectSecondItems(snapshot.data[index]);
                      },
                      child: Container(
                          color: bloc.isSelected(snapshot.data[index])
                              ? Colors.white
                              : ColorUtil.color("#f6f6f6"),
                          height: 50,
                          child: Center(
                              child: Text(
                            snapshot.data[index],
                            style: TextStyle(
                                color: bloc.isSelected(snapshot.data[index])
                                    ? theme.themeColor.themeColor
                                    : Colors.black),
                          ))),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(
                      height: 1,
                      indent: 1,
                      color: ColorUtil.color("#f6f6f6"),
                    );
                  },
                  itemCount: snapshot.data.length)
              : Container();
        },
      ),
    );
  }

  // 构建三级目录界面
  Widget _thirdListViewBulider(context) {
    return Container(
      color: Colors.white,
      child: StreamBuilder<List<ExamineBean.SubDatum>>(
        stream: bloc.thirdDataSourceSubject,
        builder: (context, snapshot) {
          return snapshot.hasData
              ? ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      onTap: () {
                        bloc.selecThirdItem(snapshot.data[index], context);
                      },
                      child: Container(
                          height: 50,
                          alignment: Alignment.centerLeft,
                          child: Padding(
                              padding: EdgeInsets.only(left: 16),
                              child:
                                  Text(snapshot.data[index].examineItemName))),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(
                      height: 1,
                      indent: 1,
                      color: ColorUtil.color("#f6f6f6"),
                    );
                  },
                  itemCount: snapshot.data.length)
              : Container();
        },
      ),
    );
  }

  @override
  ExamineListBloc generateBloc() {
    return ExamineListBloc();
  }
}
